package org.crazyit.activiti_d2._2_ListData;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.Group;

import java.util.List;

public class ListData {

    public static void main(String[] args) {

        //1. 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        //2. 得到身份服务组件实例
        IdentityService identityService = engine.getIdentityService();

        //3. 写入5条用户组数据
        createGroup(identityService,"1","GroupA","typeA");
        createGroup(identityService,"2","GroupB","typeB");
        createGroup(identityService,"3","GroupC","typeC");
        createGroup(identityService,"4","GroupD","typeD");
        createGroup(identityService,"5","GroupE","typeE");

        //4. 使用list 方法查询全部的部署数据
        List<Group> list = identityService.createGroupQuery().list();

        //5.遍历输入所有的数据
        for (Group data : list) {
            System.out.println(data.getId() + "---" + data.getName() + " ");
        }

    }

    /**
     * 将用户组数据保存到数据库中
     * @param identityService
     * @param id
     * @param name
     * @param type
     */
    public static void createGroup(IdentityService identityService, String id, String name, String type) {

        //1. 调用newGroup() 方法创建Group 实例
        Group group = identityService.newGroup(id);

        //2. 设置用户组数据
        group.setName(name);
        group.setType(type);

        //3. 保存用户组信息到数据库
        identityService.saveGroup(group);

    }

}
